package GameObjects;

import java.util.LinkedList;
import java.util.Vector;

import utils.myVector;

import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.TexCoords;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Box;
import com.jme.util.geom.BufferUtils;

public class GameObjectManager {
	
	private static GameObjectManager instance = null;
	
	private Vector<Enemy> enemyList;
	private Vector<CelestialObject> planetList;
	private Vector<Bullet> bulletList;
	private Player player;
	private int objectID=0;
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTOR
	public GameObjectManager(){
		enemyList = new Vector<Enemy>();
		planetList = new Vector<CelestialObject>();
		bulletList = new Vector<Bullet>();
	}
	
	public static GameObjectManager getGameObjectManager(){
		if(instance==null)
			instance = new GameObjectManager();
		return instance;
	}
	
	
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////// ADD OBJECT
	//////////////////////////////////////////////////////////////////////////////////////////////// PLAYER
	public void addPlayer(Node scene){
        //box stand in
		Box b = new Box("box", new Vector3f(), 0.35f,0.25f,0.5f);
		b.setModelBound(new BoundingBox());
		b.updateModelBound();
	        
        //set the player DeathStar attributes
        player = new Player("Player Node", b, "ThePlayer", new myVector(), 
        		1, //mass
        		1, //radius
        		2, //"speed"
        		10); //health
        
        player.setLocalTranslation(new Vector3f(0,0, 0));
        scene.attachChild(player);
        player.updateWorldBound();
        player.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
        
        ++objectID;
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////////// ENEMY
	public void addEnemy(Node scene){
		
		Enemy enemy;

		TriMesh square = new TriMesh("enemy_"+objectID);
		  // Vertex positions for the mesh
        Vector3f[] vertices={
            new Vector3f(0,0,0),
            new Vector3f(10,0,0),
            new Vector3f(0,0,10),
            new Vector3f(10,0,10)
        };
        // Normal directions for each vertex position
        Vector3f[] normals={
            new Vector3f(0,1,0),
            new Vector3f(0,1,0),
            new Vector3f(0,1,0),
            new Vector3f(0,1,0)
        };
        // Color for each vertex position
        ColorRGBA[] colors={
            new ColorRGBA(1,0,0,1),
            new ColorRGBA(1,0,0,1),
            new ColorRGBA(0,1,0,1),
            new ColorRGBA(0,1,0,1)
        };
 
        int[] indices={
            0,1,2,1,2,3
        };

        square.reconstruct( 
        		BufferUtils.createFloatBuffer(vertices),
        		BufferUtils.createFloatBuffer(normals),
        		BufferUtils.createFloatBuffer(colors), 
        		null,
        		BufferUtils.createIntBuffer(indices));

        float x=20,y=20;
		enemy = new Enemy("enemy_"+objectID, square, 
	    		"chupacabra", new myVector(x,y), 1, 1, 0, 1);

		
		enemy.setLocalTranslation(new Vector3f(0,0, 0));
		scene.attachChild(enemy);

		enemyList.add(enemy);
        enemy.updateWorldBound();
        enemy.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
        
        ++objectID;
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////////// CELESTIAL
	public void addCelestialObject(){
        ++objectID;
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////////// BULLET
	public void addBullet(){
        ++objectID;
	}
	
	
	

	/////////////////////////////////////////////////////////////////////////////////////////////////////////  GET FUNCTIONS
	public Player getPlayer(){
		return player;
	}
	
	public Vector<Enemy> getEnemies(){
		return enemyList;
	}
	
	public Vector<CelestialObject> getCelestialObjects(){
		return planetList;
	}
	
	public Vector<Bullet> getBullets(){
		return bulletList;
	}
}
